﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;

using System.Text;
using System.Windows.Forms;
using OpenGado.DATA;
using OpenGado.DATA.Entities;

namespace OpenGado.Win
{
    public partial class frmVendorDetails : Form
    {

        Vendor _vendor;
        Contact _contact = new Contact();
        IList<Contact> contacts = new List<Contact>();
        public delegate void VendorDetailsUpdateHandler();
        public VendorDetailsUpdateHandler UpdateVendorDetails;



        public frmVendorDetails()
        {
            InitializeComponent();
        }

        public frmVendorDetails(int vendorId)
        {
            InitializeComponent();

            _vendor = VendorDataControl.Instance.GetbyId(vendorId);
            SetVendorDetails();
        }

        private void SetContacts()
        {
            if (_contact == null) return;

            txtContactName.Text = _contact.ContactName;
            txtContactAddress.Text = _contact.ContactPostalAddress;
            txtContactEmail.Text = _contact.ContactEmail;
            txtContactPhone.Text = _contact.ContactPhone;
            txtContactMobile.Text = _contact.ContactMobile;
            txtContactRemarks.Text = _contact.ContactRemarks;
            txtContactCity.Text = _contact.ContactCity;
            txtContactCountry.Text = _contact.ContactCountry;
        }

        private void SetVendorDetails ()
        {

            txtVendorName.Text = _vendor.VendorName;
            txtFax.Text = _vendor.VendorFax;
            txtWebsite.Text = _vendor.VendorWebsite;
            txtRemarks.Text = _vendor.VendorRemarks;
            contacts = _vendor.Contact;
           
            if (contacts == null) return;
            
            _contact = contacts[0];

            SetContacts();
        }

        /// <summary>
        /// Saves vendor information
        /// </summary>
        private void SaveVendorInfo()
        {

            if (_vendor == null)
            {
                _vendor = new Vendor();
              
                
            }

            // mapping vendor fields.
            _vendor.VendorName = txtVendorName.Text;
            _vendor.VendorFax = txtFax.Text;
            _vendor.VendorWebsite = txtWebsite.Text;
            _vendor.VendorRemarks = txtRemarks.Text;
            _vendor.Contact = contacts;

            VendorDataControl.Instance.Save(_vendor);

        }


        private void btnSave_Click(object sender, EventArgs e)
        {
            SaveVendorInfo();
            SaveContactInfo();

            if (UpdateVendorDetails != null)
            {
                UpdateVendorDetails();
            }
        }
       
        private void btnClose_Click(object sender, EventArgs e)
        {
            this.Close();
        }

       
        /// <summary>
        /// Saves contact information
        /// </summary>
        private void SaveContactInfo()
        {
           
            if (_contact ==null)
            {
                _contact = new Contact();
            }
               
               _contact.Vendor = _vendor;
               _contact.ContactName = txtContactName.Text;
               _contact.ContactPostalAddress = txtContactAddress.Text;
               _contact.ContactEmail = txtContactEmail.Text;
               _contact.ContactPhone = txtContactPhone.Text;
               _contact.ContactCity = txtContactCity.Text;
               _contact.ContactCountry = txtContactCountry.Text;
               _contact.ContactMobile = txtContactMobile.Text;
               _contact.ContactRemarks = txtContactRemarks.Text;

               ContactsDataControl.Instance.Save(_contact);
              
        }

        private void PopulateContactDetails()
        {
            

            txtContactName.Text = _contact.ContactName;
            txtContactEmail.Text = _contact.ContactEmail;
            txtContactAddress.Text = _contact.ContactPostalAddress;
            txtContactCity.Text = _contact.ContactCity;
            txtContactPhone.Text = _contact.ContactPhone;
            txtContactMobile.Text = _contact.ContactMobile;
            txtContactCountry.Text = _contact.ContactCountry;
            txtContactRemarks.Text = _contact.ContactRemarks;


        }

       
       
    }
}
